package com.hunray.rent.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Email;

@Entity
@Table(name = "t_user")
public class User implements Serializable {

	private static final long serialVersionUID = 1L;

	private Integer id;
	
	private String no;

	private String username;

	private String password;

	private String salt;

	private String email;
	
	private String phone;
	
	private String creater;

	private Boolean locked;
	
	private Boolean deleted;

	private int status;
	
	private Date createDate;
	
	private Department department;
	
	private Group group;
	
	private Account account;
	
	private List<UserRole> userRoles = new ArrayList<UserRole>();
	
	private List<Rent> rents;
	
	@Id
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "tg_user")
	@TableGenerator(name = "tg_user", table = "t_id", pkColumnName = "f_table", pkColumnValue = "t_user", valueColumnName = "f_id", allocationSize = 1, initialValue = 1)
	@Column(name = "f_id")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	@Column(name="f_no",nullable=false,unique=true)
	@NotNull
	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	@Column(name = "f_username", nullable = false, unique = true)
	@NotNull
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name = "f_password", nullable = false)
	@NotNull
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "f_salt")
	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}

	@Email
	@Column(name = "f_email")
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name="f_phone")
	@NotNull
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	@Column(name="f_creater")
	public String getCreater() {
		return creater;
	}

	public void setCreater(String creater) {
		this.creater = creater;
	}
	@Column(name = "f_is_locked", length = 1,nullable=false)
	public Boolean getLocked() {
		return locked;
	}

	public void setLocked(Boolean locked) {
		this.locked = locked;
	}
	
	@Column(name="f_is_deleted")
	public Boolean getDeleted() {
		return deleted;
	}

	public void setDeleted(Boolean deleted) {
		this.deleted = deleted;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "f_create_date")
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@Column(name="f_status")
	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="f_department")
	public Department getDepartment() {
		return department;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="f_group")
	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	@OneToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="f_account")
	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
	public List<UserRole> getUserRoles() {
		return userRoles;
	}

	public void setUserRoles(List<UserRole> userRoles) {
		this.userRoles = userRoles;
	}
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "renter")
	public List<Rent> getRents() {
		return rents;
	}

	public void setRents(List<Rent> rents) {
		this.rents = rents;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "User [username=" + username + "]";
	}

	@Transient
	public List<Role> getRoles() {
		List<Role> roles = new ArrayList<Role>();
		for (UserRole userRole : getUserRoles()) {
			roles.add(userRole.getRole());
		}
		return roles;
	}

	@Transient
	public List<String> getRoleNames() {
		List<String> roleNames = new ArrayList<String>();
		for (Role role : getRoles()) {
			roleNames.add(role.getName());
		}
		return roleNames;
	}

	@Transient
	public List<String> getRoleIdentityNames() {
		List<String> roleIdentityNames = new ArrayList<String>();
		for (Role role : getRoles()) {
			roleIdentityNames.add(role.getIdentityName());
		}
		return roleIdentityNames;
	}

	@Transient
	public List<Permission> getPermissions() {
		List<Permission> permissions = new ArrayList<Permission>();
		for (Role role : getRoles()) {
			permissions.addAll(role.getPermissions());
		}
		return permissions;
	}

	@Transient
	public List<String> getPermissionNames() {
		List<String> permissionNames = new ArrayList<String>();
		for (Role role : getRoles()) {
			permissionNames.addAll(role.getPermissionNames());
		}
		return permissionNames;
	}
}
